package org.dromara.system.service.impl.trading.channel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.system.config.core.RedisDelayMQSender;
import org.dromara.system.converter.MachineConverters;
import org.dromara.system.domain.ReceiveDealRecordsReqDTO;
import org.dromara.system.domain.ReceiveDealRecordsReqDTO;
import org.dromara.system.domain.TradingRecords;
import org.dromara.system.enums.NotificationEnum;
import org.dromara.system.enums.RedisMQConstant;
import org.dromara.system.service.impl.trading.AbstractTradingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service("kYTTradingService")
@RequiredArgsConstructor
public class KYTTradingService extends AbstractTradingService<Object, ReceiveDealRecordsReqDTO> {

    @Resource
    private RedisDelayMQSender redisDelayMQSender;

    @Override
    public NotificationEnum getNotificationEnum() {
        return NotificationEnum.KYT;
    }


    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void tradingWaterConvert(ReceiveDealRecordsReqDTO reqDTO) {
        TradingRecords tradingRecords = MachineConverters.INSTANCE.receiveDealRecordsToReceiveDealRecords(reqDTO);
        String dealType = reqDTO.getDealType();
        if (StrUtil.equals(dealType,"DEBIT_CARD")){
            tradingRecords.setIsDebitCard(Boolean.TRUE);
        }
        R<TradingRecords> tradingRecordsR = dealUserTrading(tradingRecords);
    }


    @Override
    public Object tradingWaterAssemble(String mapBody) {
        log.info("快易通交易参数={}", JSONUtil.toJsonStr(mapBody));
        ReceiveDealRecordsReqDTO receiveDealRecords = JSONUtil.toBean(mapBody, ReceiveDealRecordsReqDTO.class);
        String orderCode = receiveDealRecords.getOrderCode();
        redisDelayMQSender.send(RedisMQConstant.TRADING_QUEUE, mapBody, getNotificationEnum(), orderCode);

        return Boolean.TRUE;

    }

    public static void main(String[] args) {
        String string = "{\"id\":2294040,\"brandId\":17,\"lineId\":28,\"terminalId\":190027,\"sn\":\"0000310272220406583488463B\",\"merchantCode\":\"110000011138571\",\"merchantName\":\"个体户王宝霞\",\"agentId\":5293,\"topCodeDeal\":\"100012465\",\"dealMoney\":3680,\"settlesAmountMoney\":3659.76,\"serviceMoney\":20.24,\"dealType\":\"CREDIT_CARD\",\"orderCode\":\"105997040603\",\"dealCompleteTime\":\"2025-08-1514:45:54\",\"dealDate\":1755240354000,\"dealRate\":\"0.55\",\"edition\":0,\"createTime\":1755240373608,\"createBy\":0,\"updateTime\":1755240373608,\"brand_tag\":\"tool\",\"send_url\":\"http:\\/\\/121.40.66.143:4330\\/system\\/trading\\/KYT\\/tradingWaterAssemble\"}";
        ReceiveDealRecordsReqDTO bean = JSONUtil.toBean(string, ReceiveDealRecordsReqDTO.class);
        TradingRecords tradingRecords = MachineConverters.INSTANCE.receiveDealRecordsToReceiveDealRecords(bean);
        System.out.println(JSONUtil.toJsonStr(tradingRecords));
    }


}
